<?php
class articleModel extends baseModel{
    protected $table = '';  //文章表
    //读取扩展模型ID
    public function plusmid($cid){
        $this->table = model('channel')->classModel($cid);
        $rel = model('channel')->find(array('id'=>$cid),'plus_mid');
        if($rel['plus_mid']){
            return $rel['plus_mid'];
        }
        return FALSE;
    }
    //判断列表
    public function lists($fid,$all,$limit){
        $this->table = model('channel')->classModel($fid);
        $rel = $this->select('cid in('.$all.')', 'cid,title,id,uptime,views,sort,status,fontcolor', 'id desc', $limit);
        return $rel;
    }
    //统计条数
    public function counts($fid,$all){
        $this->table = model('channel')->classModel($fid);//判断模型
        return $this->count('cid in('.$all.')');
    }
   //编辑内容
   public function editInfo($cid,$fid){
        $cid = intval($cid);
        $fid = intval($fid);
        $tables = model('channel')->classModel($cid);
        $this->table = $tables;
        $condition['id']=$fid;
        $condition['cid']=$cid;
        $info = $this->find($condition);
        $this->table = $tables.'_data';
        $infoplus = $this->find($condition);
        if($info && $infoplus){
            return array_merge($info,$infoplus);
        }
        return FALSE;
   }
    //获取添加表单
   public function addInput($mid){
        $rel  = model('field')->fromInput($mid);
        return $rel ;
   }
   //获取编辑表单
   public function editInput($mid,$input_value){
        $rel  = model('field')->fromInput($mid,TRUE,$input_value);
        return $rel;
   }
    //删除文章
   public function delInfo($cid,$fid){
        $tables = model('channel')->classModel($cid);
        $result = $this->model->table($tables)->where('id='.$fid)->delete();
        if($result){
            $rel = $this->model->table($tables.'_data')->where('id='.$fid)->delete();
            $ext = model('channel')->find('id='.$cid,'plus_mid');
            if($ext['plus_mid']){
                $ext_tables = model('module')->tables($ext['plus_mid']);
                $this->model->table($ext_tables)->where('fid='.$fid.' and cid='.$cid)->delete();
                $this->model->table($ext_tables.'_data')->where('fid='.$fid.' and cid='.$cid)->delete();
            }
        }
        if($rel){
            return TRUE;
        }else{
            return FALSE;
        }
   }
   //内容增加
   public function dataSave($mid,$postinput,$fid = null){
        $mid  = intval($mid);
        if(!$mid){
           return;
        }
        $data = array();
        //系统主表
        $data['cid'] = $postinput['cid'];
        $data['title'] = $postinput['title'];
        $data['sort'] = $postinput['sort'];
        $data['views'] = $postinput['views'];
        $data['url'] = $postinput['url'];
        $data['special'] = $postinput['special'];
        $data['uptime'] = $postinput['uptime'];
        $data['editor'] = $postinput['editor'];
        $data['fontcolor'] = $postinput['fontcolor'];
        $data['status'] = 1;
        if($postinput['special']){
            $data['special'] = implode(',', $postinput['special']);
            $data['special'] .= ',';
        }
        //判断入库所在表
        $fieids = model('field')->select('mid='.$mid,'fields,tbdata');
        foreach ($fieids as $key => $value) {
          if($value['tbdata']){
              $extdata[$value['fields']] = $postinput[$value['fields']];//副表
          }else{
              $data[$value['fields']] = $postinput[$value['fields']];//主表
          }
        }
        //入库
        $this->table = model('module')->tables($mid);
        if($fid){
            $condition['id']=$fid;
            $this->update($condition,$data);
            $this->table = $this->table.'_data';
            $extdata['cid'] = $postinput['cid'];
            $info = $this->update($condition,$extdata);
        }else{
            $this->insert($data);
            $extdata['id'] = mysql_insert_id();
            $extdata['cid'] = $postinput['cid'];;
            $this->table = $this->table.'_data';
            $info = $this->insert($extdata);
        }
        return $info ;
   }
   //列表页更新
   public function states($type,$cid,$fid,$state){
        if($state){
            $state = 0;
        }else{
            $state = 1;
        }
        switch ($type) {
            case 'sort':
                $data['sort'] = $state;
                break;
            case 'stat':
                $data['status'] = $state;
                break;
            default:
                return FALSE;
                break;
        }
        $tables = model('channel')->classModel($cid);
        $this->table = $tables;
        $condition['id'] = $fid;
        $rel = $this->update($condition,$data);
        if($rel){
            return TRUE;
        }else{
            return FALSE;
        }
   }
    //判断是否有扩展
    public function extsListsCount($title,$cid,$fid){
        $title = $title;
        $cid = intval($cid);
        $fid = intval($fid);
        $rel = model('article')->plusmid($cid);
        if($rel){
            $count = model('article')->extcounts($rel,$cid,$fid);
            echo ' <a title="扩展内容" class="red fn-fb" href="'.url('article/extlists',array('mid'=>$rel,'cid'=>$cid,'fid'=>$fid,'title'=>$title)).'">[ '.$count.' ]</a>';
        }
    }
    //扩展内容列表
    public function extlists($mid,$aid){
        $this->table = model('module')->tables($mid);
        $rel = $this->select('fid='.$aid, 'id,fid,title,uptime,fontcolor', 'id desc');
        return $rel;
    }
    //统计有多少内容
    public function extcounts($mid,$cid,$fid){
        $this->table = model('module')->tables($mid);
        return $this->count('fid='.$fid.' and cid='.$cid);
    }
    //扩展内容增加
   public function extsSave($mid,$postinput,$fid = null){
        $this->table = model('module')->tables($mid);
        //系统主表
        $data = array();
        $data['fid'] = $postinput['fid'];
        $data['cid'] = $postinput['cid'];
        $data['title'] = $postinput['title'];
        $data['fontcolor'] = $postinput['fontcolor'];
        $data['uptime'] = time();
        //判断入库所在表
        $fieids = model('field')->select('mid='.$mid,'fields,tbdata');
        foreach ($fieids as $key => $value) {
          if($value['tbdata']){
              $extdata[$value['fields']] = $postinput[$value['fields']];//副表
          }else{
              $data[$value['fields']] = $postinput[$value['fields']];//主表
          }
        }
        if($fid){
            $condition['id']=$fid;
            $this->update($condition,$data);
            $this->table = $this->table.'_data';
            $extdata['cid'] = $postinput['cid'];
            $info = $this->update($condition,$extdata);
        }else{
            $this->insert($data);
            $extdata['id'] = mysql_insert_id();
            $extdata['fid'] = $postinput['fid'];;
            $extdata['cid'] = $postinput['cid'];;
            $this->table = $this->table.'_data';
            $info = $this->insert($extdata);
        }
        return $info ;
   }
   //编辑内容
   public function extseditInfo($mid,$fid){
        $mid = intval($mid);
        $fid = intval($fid);
        $tables = model('module')->tables($mid);
        $this->table = $tables;
        $condition['id']=$fid;
        $info = $this->find($condition);
        $this->table = $tables.'_data';
        $infoplus = $this->find($condition);
        if($info && $infoplus){
            return array_merge($info,$infoplus);
        }
        return FALSE;
   }
    //删除文章
   public function extdelInfo($mid,$fid){
        $tables = model('module')->tables($mid);
        $this->table = $tables;
        $delrel = $this->delete('id='.$fid);
        if($delrel ){
            $this->table = $tables.'_data';
            $rel = $this->delete('id='.$fid);
        }
        if($rel){
            return TRUE;
        }else{
            return FALSE;
        }
   }
    //##########################################################
    //前台栏目标签算法
    public function ArticleLabel($get = array(),$cid = 0,$pages = 0,$pageurl =0){
        //判断是否锁定和当前栏目
        if($get['es_locks'] <> 2){//判断是否模型ID
            if($get['es_subclass']){//是否包含子类
                $pid = ' and '.$get['es_class'].' in ('.model('channel')->AllChildClass($cid).')';
            }else{
                $pid = ' and '.$get['es_class'].' = '.$cid;
            }
            $tables = model('channel')->classModel($cid);//根据栏目ID判断当前表
        }else{
            $tables = model('module')->tables($cid);//根据栏目ID判断当前表
        }
        if(!empty($get['es_special'])){//资源特性
            $special = ' and special like \'%'.intval($get['es_special']).',%\'';
        }
        if(!empty($get['es_where'])){//SQL条件
            $where = ' and '.$get['es_where'];
        }
        $limits = intval($get['es_limits']);//翻页
        if(empty($get['es_limits'])){
            $limits = 10;
        }
        $order = $get['es_order'];//排序
        if(empty($get['es_order'])){
            $order = 'id desc';
        }
        $this->table = $tables; 
        if($pages){//分页计算
            $listRows = $limits; //每页条数
            $url = $pageurl;
            $allcount = $this->count('status=1'.$pid.$special.$where);
            $limits = $this->pageLimit($url,$listRows);
            $rel[1]= $this->pageShow($allcount);
        }
        $info = model('article')->select('status=1'.$pid.$special.$where,null,$order,$limits);
        if($pages){
            $rel[0] = $info;
        }else{
            $rel = $info;
        }
        return $rel;
   }
   //内容单独调用
   public function ArticleDisplay($fid,$cid){
        //根据栏目ID判断当前表
        $tables = model('channel')->classModel($cid);
        if($tables){
            $rel = $this->model->table($tables)->where('status=1 and id='.$fid)->find();
            if($rel){
                $rel_ext = $this->model->table($tables.'_data')->where('id ='.$rel['id'])->find();
                $del = array('sort','status','special');
                if($rel && $rel_ext){
                    $result = postinput(array_merge($rel,$rel_ext),$del);
                    $data['views'] = $rel['views']+1;
                    $this->model->table($tables)->where('status=1 and id='.$fid)->data($data)->update();
                }
            }
        }
        return $result;
    }
    //内容单独调用__附加表
    public function ArticleList_Plus($fid,$mid){
        //根据模型ID判断当前表
        $tables = model('module')->tables($mid);
        if($tables){
            $rel = $this->model->table($tables)->where('fid ='.$fid)->select();
            if($rel){
                foreach ($rel as $value) {
                    $rel_ext = $this->model->table($tables.'_data')->where('id ='.$value['id'])->find();
                    $result[] = array_merge($value,$rel_ext);
                } 
            }
        }
        return $result;
    }
    //内容单独调用__附加表
    public function ArticleDisplay_Plus($aid,$mid){
        //根据模型ID判断当前表
        $tables = model('module')->tables($mid);
        if($tables){
            $rel = $this->model->table($tables)->where('id ='.$aid)->find();
            if($rel){
                $rel_ext = $this->model->table($tables.'_data')->where('id ='.$rel['id'])->find();
                if($rel_ext){
                    $result = array_merge($rel,$rel_ext);
                }
            }
        }
        return $result;
    }    
    //获取分页查询limit
    protected function pageLimit($url, $num = 10){
        $url = str_replace(urlencode('{page}'), '{page}', $url);
        $page = is_object($this->pager['obj']) ? $this->pager['obj'] : new Page();  
        $cur_page = $page->getCurPage($url);
        $limit_start = ($cur_page-1) * $num;
        $limit = $limit_start.','.$num;
        $this->pager = array('obj'=>$page, 'url'=>$url, 'num'=>$num, 'cur_page'=>$cur_page, 'limit'=>$limit);
        return $limit;
    }
    //分页结果显示
    protected function pageShow($count){
        return $this->pager['obj'] ->show($this->pager['url'], $count, $this->pager['num']);
    }
}